package com.seari.tasks;

import com.dingtalk.api.DingTalkClient;
import com.seari.constant.Const;
import com.seari.entity.ArticleInfo;
import com.seari.entity.PlannedCurrentLimitation;
import com.seari.service.ArticleService;
import com.seari.service.PlannedCurrentLimitationService;
import com.seari.utils.DateUtil;
import com.seari.utils.DingUtil;
import com.seari.utils.StringUtil;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 钉钉-智能填表任务
 * 定时调取钉钉数据入库
 */
@Component
@EnableScheduling
@Slf4j
public class ArticleTasks {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private PlannedCurrentLimitationService plannedCurrentLimitationService;

    //    private final String cronVal = "0 */5 * * * ?";//每5分钟执行一次
    private final String cronVal = "*/30 * * * * ?";//每30秒执行一次


    /**
     * 财务资金保障-任务
     */
    @Scheduled(cron = cronVal)
    public void finaFundGuarTasks() {
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.CWZJBZ_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("cwzjbz"));
                articleService.insert(articleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("财务资金保障任务执行失败：" + e.getMessage());
        }


    }

    /**
     * 责任落实督察情况-任务
     */
    @Scheduled(cron = cronVal)
    public void respImplAndSuperAsks() {
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.ZRLSDC_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("zrlsdc"));
                articleService.insert(articleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("责任落实督察情况任务执行失败：" + e.getMessage());
        }
    }

    /**
     * 运营组织疫情防控-任务
     */
    @Scheduled(cron = cronVal)
    public void OperOrgaEpidPrevAndContAsks() {
        log.info("运营组织疫情防控-任务");
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
//            String res = DingUtil.getInstanceList(accessToken,Const.YYZZYQFK_FORM_CODE,DateUtil.getNowDate(),offSet,size);
            String res = DingUtil.getInstanceList(accessToken, Const.YYZZYQFK_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("yyzzyqfk"));
                articleService.insert(articleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("运营组织疫情防控任务执行失败：" + e.getMessage());
        }
    }

    /**
     * 舆情与宣传报道工作-任务
     */
    @Scheduled(cron = cronVal)
    public void publOpinAndPublRepoAsks() {
        log.info("舆情与宣传报道工作-任务");
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.YQYXCBD_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("yqyxcbd"));
                articleService.insert(articleList);
//                System.out.println(1234);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("舆情与宣传报道工作任务执行失败：" + e.getMessage());
        }
    }


    /**
     * 计划限流（审批）-任务
     */
    @Scheduled(cron = cronVal)
    public void planCurrLimiAsks() {
        String accessToken = DingUtil.getAccessToken();
        try {
            //每30秒获取一次近30秒的数据
            List<Map<String, String>> resList = DingUtil.getFormatConversionApp(accessToken, DateUtil.getStartDate(new Date()), DateUtil.getEndDate(new Date()), 20L, 0L, null);
            for (int i = 0; i < resList.size(); i++) {
                //获取审批实例详情
                String res = DingUtil.getSingleInstanceDetails(resList.get(i).get("processCode"), accessToken);
                if (StringUtil.isNotEmpty(res)) {
                    List<PlannedCurrentLimitation> pclList = DingUtil.formatConversionPcl(res);
                    plannedCurrentLimitationService.insert(pclList);
//                    System.out.println(11111111);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("计划限流任务执行失败：" + e.getMessage());
        }
    }


    /**
     * 李浩言userId:280149515826318213
     * @param args
     */
    public static void main(String[] args) throws ApiException {
        System.out.println("开始执行");
        //获取所有填表
//        String res = DingUtil.getFormList(DingUtil.getAccessToken(),0L,"280149515826318213",0L,200L);
//        System.out.println(res);
        //获取单个填表实例信息
//        respImplAndSuperAsksByNew();
//        planCurrLimiAsks();
//        finaFundGuarTasks();
    }


    //=======================================新的填表=================================

    /**
     * 责任落实督察情况-任务
     */
    @Scheduled(cron = cronVal)
    public void respImplAndSuperAsksByNew() {
        log.info("责任落实督察情况-任务");
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.ZRLSDC_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("zrlsdc"));
//                System.out.println(articleList);
                articleService.insert(articleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("责任落实督察情况任务执行失败：" + e.getMessage());
        }
    }

    /**
     * 运营组织疫情防控-任务
     */
    @Scheduled(cron = cronVal)
    public void OperOrgaEpidPrevAndContAsksByNew() {
        log.info("运营组织疫情防控-任务");
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.YYZZYQFK_FORM_CODE, DateUtil.getNowDate(), offSet, size);
//            String res = DingUtil.getInstanceList(accessToken,Const.YYZZYQFK_FORM_CODE_BY_NEW,"2020-05-12",offSet,size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("yyzzyqfk"));
                articleService.insert(articleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("运营组织疫情防控任务执行失败：" + e.getMessage());
        }
    }

    /**
     * 舆情与宣传报道工作-任务
     */
    @Scheduled(cron = cronVal)
    public void publOpinAndPublRepoAsksByNew() {
        log.info("舆情与宣传报道工作-任务");
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.YQYXCBD_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("yqyxcbd"));
                articleService.insert(articleList);
//                System.out.println(1234);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("舆情与宣传报道工作任务执行失败：" + e.getMessage());
        }
    }

    @Scheduled(cron = cronVal)
    public void staffAsksByNew() {
        log.info("员工队伍疫情防控工作-任务");
        String accessToken = DingUtil.getAccessToken();
        long offSet = 0l;
        long size = 100l;
        try {
            String res = DingUtil.getInstanceList(accessToken, Const.YGDWYQFK_FORM_CODE, DateUtil.getNowDate(), offSet, size);
            if (StringUtil.isNotEmpty(res)) {
                List<ArticleInfo> articleList = DingUtil.formatConversion(res, Const.ARTICLE_TYPE.get("ygdwyqfk"));
                articleService.insert(articleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("员工队伍疫情防控工作任务执行失败：" + e.getMessage());
        }
    }

}
